/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "../guidefaults.h"
#include "../Table.h"

namespace gui {

#define INITSIZE 10

Table::Table(unsigned int numColumns)
:	ISelectable(), 
	m_uiNumColumns(numColumns)
{
	// Allocate the columnwidths
	m_pColumnWidth = new int[numColumns];

	int size = 0;
	for (unsigned int i=0; i<m_uiNumColumns; ++i) {
		m_pColumnWidth[i] = INITSIZE;
		size += INITSIZE;
	}	
	setSize(size, INITSIZE);
}

Table::~Table(void)
{
	DELETE_ARRAY( m_pColumnWidth );
}

void Table::setColumnWidth(unsigned int columnIndex, int width)
{
	if (columnIndex >= m_uiNumColumns) 
		return;	

	m_pColumnWidth[columnIndex] = width;

	resize();
}

void Table::resize()
{
	int size = 0;
	for (unsigned int i=0; i<m_uiNumColumns; ++i) 
		size += m_pColumnWidth[i];
	// Set the size to the sum of the column width and number of rows. Min is 1 row
	setSize( size, max(iRowSize, static_cast<int>(iRowSize*components.size())) );
}

void Table::addItem(TableItem *item)
{
	Component::add(item);
	resize();
}

void Table::clear()
{
	clearAll();
	m_iIdSelected = -1;
	resize();
}

TableItem* Table::getSelectedItem() const 
{
	if (m_iIdSelected == -1) 
		return NULL;

	return (TableItem*) components.at(m_iIdSelected);
}

void Table::draw()
{
	gui::TableItem *item;
	comp_list::iterator it;
	unsigned int col;
	int row;
	int colPos;

	if (bVisible) {
		vRealPos = getAbsolutePosition();
		pRenderer->drawRect(vRealPos.x, vRealPos.y, vDim.x, vDim.y, vBorderColor);

		row = 0;
		// Draw every item in the list
		for (it = components.begin(); it != components.end(); ++it) {
			item = (TableItem*) *it;
			colPos = 0;
			if (isSelectable() && row == m_iIdSelected) {
				pRenderer->drawFilledRect(vRealPos.x, vRealPos.y + (iRowSize*row), vDim.x, iRowSize, vSelectedBackColor);
				pRenderer->drawRect(vRealPos.x, vRealPos.y + (iRowSize*row), vDim.x, iRowSize, vSelectedBorderColor);
			}

			for (col = 0; col < m_uiNumColumns; ++col) {
				
				if (!item->getString(col).empty())
					font->print( item->getString(col), vRealPos.x + colPos + vOffset.x, vRealPos.y+(iRowSize*row)+vOffset.y, 0, FONT_LEFT, 0, 0, 
								 ((isSelectable() && row == m_iIdSelected) ? vSelectedTextColor : vTextColor)); 
			
				/*
				if (item->getItem(col))
					item->getItem(col)->draw();
				*/
				colPos += m_pColumnWidth[col];
			}
			++row;
		}	

		// Draw the column lines
		colPos = 0;
		for (col=0; col<m_uiNumColumns; ++col) {
			pRenderer->drawFilledRect(vRealPos.x+colPos, vRealPos.y, 1, vDim.y, vBorderColor);
			colPos += m_pColumnWidth[col];
		}
	}
}

void Table::onMousePressed(const MouseEvent &me)
{
	Component::onMousePressed(me);
	m_iIdSelected = (me.y - vRealPos.y) / iRowSize;
}


} // namespace
